<%--
  Created by IntelliJ IDEA.
  User: ddd
  Date: 2018/4/22
  Time: 16:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户选题</title>
    <link href="/static/css/bootstrap.min.css" rel="stylesheet">
    <link href="/static/css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/static/css/font-awesome.min93e3.css">
    <link rel="stylesheet" type="text/css" href="/static/css/admin.css">
</head>
<body>
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="admin-container">
        <input type="hidden" class="userids">
        <table id="cartypetable" data-mobile-responsive="true"></table>
        <button class="btn btn-success" onclick="AddPower()">确定</button>
    </div>

</div>
<script src="/static/js/jquery.min.js"></script>
<script src="/static/layer/layer.js"></script>
<script src="/static/js/bootstrap.min.js"></script>
<script src="/static/js/plugins/bootstrap-table/bootstrap-table.js"></script>
<script src="/static/js/plugins/bootstrap-table/bootstrap-table-mobile.js"></script>
<script src="/static/js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
<script>
    function courseSelect(id) {
        $(".course" + id).each(function () {
            if (!$(".mcourse" + id).prop("checked")) {
                $(this).prop("checked", false);
            }
            else {
                $(this).prop('checked',true);

            }
        });
    }
    function carSelect(id) {
        $(".car" + id).each(function () {
            if (!$(".mcar" + id).prop("checked")) {
                $(this).prop("checked", false);
            }
            else {
                $(this).prop('checked',true);

            }
        });
    }
    $.ajax({
        type: 'GET',
        dataType: "json",
        url: '/levelAndUserLe',
        success: function (result) {
            console.info(result);

            $('#cartypetable').bootstrapTable({
                data: result.allvos,
                method: 'GET',
                striped: true,
                cache: false,
                height: 690,
                pagination: true,
                sidePagination: "client",
                pageNumber: 1,
                pageSize: 15,
                pageList: [10, 15, 25],
                clickToSelect: true,
                uniqueId: "id",
                columns: [
                    {
                        title: '车型',
                        formatter: function (value, row) {
                            var dd = $("<div>");
                            var $span = $("<span>");
                            var $check = $("<input type=\"checkbox\" checked>").addClass("mcar"+row.id).attr("onchange"," carSelect("+row.id+")");
                            dd.append($("<label>").addClass("car").append($check).append("<input type=\"hidden\" class='carid' value=" + row.id + ">").append($span));
                            $span.html(row.title);
                            return dd.html();
                        }
                    },
                    {
                        field: '',
                        title: '科目',
                        formatter: function (value, row) {
                            var courseData = row.courseVos;

                            //对courseData对象进行重新排序
                            function JsonSort(courseData, key) {
                                for (var j = 1; j < courseData.length; j++) {
                                    var temp = courseData[j],
                                        val = temp[key],
                                        i = j - 1;
                                    while (i >= 0 && courseData[i][key] > val) {
                                        courseData[i + 1] = courseData[i];
                                        i = i - 1;
                                    }
                                    courseData[i + 1] = temp;
                                }
                                return courseData;
                            }

                            var courseData = JsonSort(courseData, 'id');
//                            console.info(courseData);

                            var $div = $("<div>");
                            var $course = $("<table>").addClass("table");
                            $div.append($course);
                            for (var j = 0; j < courseData.length; j++) {
                                var $tr = $("<tr>");
                                $course.append($tr);
                                var $coCheck = $("<input type=\"checkbox\" checked>").addClass("car" + row.id).addClass("mcourse" + courseData[j].id).attr("onchange", "courseSelect(" + courseData[j].id + ")");
                                var $td1 = $("<td>").append($("<label>").addClass("course").append($coCheck).append("<input type=\"hidden\" class='courseid' value=" + courseData[j].id + ">").append($("<i>").addClass("fa fa-file-text").html(courseData[j].name + "(" + courseData[j].exertypes.length + ")")));
                                $tr.append($td1);
                                var $td2 = $("<td>");
                                $tr.append($td2);
                                for (var i = 0; i < courseData[j].exertypes.length; i++) {
                                    if (courseData[j].exertypes[i].status === 1) {
                                        var $eCheck=$("<input type=\"checkbox\" checked>").addClass("car"+row.id).addClass("course"+courseData[j].id);
                                        $td2.append($("<label>").addClass("exer").append($eCheck).append("<input type=\"hidden\" class='exerid' value=" + courseData[j].exertypes[i].id + ">").append("[" + courseData[j].exertypes[i].px + "]" + courseData[j].exertypes[i].name));
                                    }
                                }
                            }
                            return $div.html();
                        }
                    }]
            });


        }
    });


    function getuserid(ids) {
        $(".userids").val(ids);
    }

    function AddPower() {
        var data = new Object();
        var userids = $(".userids").val();
        var ids = [];
        ids = userids.split(",");
        var levels = [];
        data.ids = ids;
        var count = 0;
        var $car = $(".car");
        $.each($car, function (i, e) {

            var ischecked = $(this).find("input[type='checkbox']").is(':checked');
            if (ischecked) {
                levels[count] = new Object();
                var lid = $(this).find(".carid").val();
                levels[count].lid = lid;
                levels[count].type = 3;
                count++;
            }

//            console.info(levels);
        });


        var $course = $(".course");
        $.each($course, function (i, e) {
            var ischecked = $(this).find("input[type='checkbox']").is(':checked');
            if (ischecked) {
                levels[count] = new Object();
                var lid = $(this).find(".courseid").val();
                levels[count].lid = lid;
                levels[count].type = 2;
                count++;
            }
        });


        var $exer = $(".exer");
        $.each($exer, function (i, e) {

            var ischecked = $(this).find("input[type='checkbox']").is(':checked');
            if (ischecked) {
                levels[count] = new Object();
                var lid = $(this).find(".exerid").val();
                console.info(lid);
                levels[count].lid = lid;
                levels[count].type = 1;
                count++;
            }

        });

        data.levels = levels;


        $.ajax({
            contentType: 'application/json;charset=UTF-8',
            type: 'PUT',
            dataType: "json",
            data: JSON.stringify(data),
            url: '/level',
            success: function (result) {
                (function () {
                    layer.msg('操作成功', {icon: 1,time:1000});
                    setTimeout(function () {
                        var ind = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(ind);
                    },1000);
                })();
            }
        });
    }
</script>
</body>
</html>
